package deweb.cimaplanning.beans;

import java.util.ArrayList;
import java.util.Hashtable;

import deweb.cimaplanning.misc;
import deweb.cimaplanning.dao.POCostsDao;
import deweb.cimaplanning.dto.POCosts;
import deweb.cimaplanning.dto.POCostsByElements;
import deweb.cimaplanning.dto.PORecurrentCost;

public class POCostsBean {
	protected boolean successful;
	protected String errorMsg;
	
	protected Hashtable costsByPO;
	protected Hashtable costsByPORecurrent;
	protected Hashtable costsByPOWarehouse;
	
	private POCostsDao dao = null;
	
	public POCostsBean(){
		dao = new POCostsDao();
	}
	
	public ArrayList browse(){
		return browse(0, misc.ALL, true);
	}
	
	public ArrayList browse(int versionId, String filtersChain, boolean in){
		ArrayList results	= null;
		String sql			= "";
		
		if (versionId > 0)
			sql = "	pov.version_id = " + versionId + " ";
		
		if (filtersChain.equals(misc.ALL))
			sql += "		AND ce.short_name NOT IN ('" + misc.USD_PER_TON + "', '" + misc.LOSS_FUTURES + "')  ";
		else {
			sql += "		AND ce.short_name " + (in ? "" : "NOT") + " IN ('" + filtersChain + "')  ";
		}
		
		try {
			results = dao.getArray(sql);
			
			successful	= true;
			errorMsg	= "";
		} catch (Exception e) {
			successful	= false;
			errorMsg	= e.getMessage();
			e.printStackTrace();
			return null;
		}
		
		return results;
	}
	
	public POCosts get(int purchaseOrderVersionId, String filtersChain, boolean in){
		POCosts results	= null;
		String sql			= "";
		
		if (filtersChain.equals(misc.ALL))
			sql += "		AND ce.short_name NOT IN ('" + misc.USD_PER_TON + "', '" + misc.LOSS_FUTURES + "')  ";
		else {
			sql += "		AND ce.short_name " + (in ? "" : "NOT") + " IN ('" + filtersChain + "')  ";
		}
		
		try {
			results = dao.getById(purchaseOrderVersionId, sql);
			
			successful	= true;
			errorMsg	= "";
		} catch (Exception e) {
			successful	= false;
			errorMsg	= e.getMessage();
			e.printStackTrace();
			return null;
		}
		
		return results;
	}
	
	public POCosts getPOIdCEId(int purchaseOrderVersionId, String costElementCode){
		POCosts results	= null;
		
		try {
			results = dao.getByPOIdCEId(purchaseOrderVersionId, costElementCode);
			
			successful	= true;
			errorMsg	= "";
		} catch (Exception e) {
			successful	= false;
			errorMsg	= e.getMessage();
			e.printStackTrace();
			return null;
		}
		
		return results;
	}
	
	public void getAllByPOIdCEId(int versionId){
		ArrayList results	= null;
		costsByPO			= new Hashtable(100);
		
		POCostsByElements dto = null;
		
		try {
			results = dao.getAllByPOIdCEId(versionId);
			
			for (int i = 0; i < results.size(); i++){
				dto = (POCostsByElements)results.get(i);
				
				costsByPO.put(dto.getPurchaseOrderVersionId() + "|" + dto.getShortName(), new Double(dto.getAmount()));
			}
			
			successful	= true;
			errorMsg	= "";
		} catch (Exception e) {
			successful	= false;
			errorMsg	= e.getMessage();
			e.printStackTrace();
		}
	}
	
	public Double costPOIdCEId(int purchaseOrderVersionId, String shortName){
		return (Double)costsByPO.get(purchaseOrderVersionId + "|" + shortName);
	}
	
	public void getAllByPOIdCEIdRecurrent(int versionId, int costOrigin){
		ArrayList results	= null;
		costsByPORecurrent	= new Hashtable(100);
		
		PORecurrentCost dto = null;
		
		try {
			results = dao.getAllByPOIdCEIdRecurrent(versionId, costOrigin);
			
			for (int i = 0; i < results.size(); i++){
				dto = (PORecurrentCost)results.get(i);
				
				costsByPORecurrent.put(dto.getPurchaseOrderVersionId() + "|"
										+ dto.getCostElementShortName() + "|"
										+ dto.getPlantVersionId(), new Double(dto.getAmount()));
			}
			
			successful	= true;
			errorMsg	= "";
		} catch (Exception e) {
			successful	= false;
			errorMsg	= e.getMessage();
			e.printStackTrace();
		}
	}
	
	public Double costPOIdCEIdRecurrent(int purchaseOrderVersionId, String shortName, int plantVersionId){
		return (Double)costsByPORecurrent.get(purchaseOrderVersionId + "|" + shortName + "|" + plantVersionId);
	}
	
	public void getAllByPOIdCEIdWarehouse(int versionId){
		ArrayList results	= null;
		costsByPOWarehouse	= new Hashtable(100);
		
		PORecurrentCost dto = null;
		
		try {
			results = dao.getAllByPOIdCEIdWarehouse(versionId);
			
			for (int i = 0; i < results.size(); i++){
				dto = (PORecurrentCost)results.get(i);
				
				costsByPOWarehouse.put(dto.getPurchaseOrderVersionId() + "|"
										+ dto.getPlantVersionId(), new Double(dto.getAmount()));
			}
			
			successful	= true;
			errorMsg	= "";
		} catch (Exception e) {
			successful	= false;
			errorMsg	= e.getMessage();
			e.printStackTrace();
		}
	}
	
	public Double costPOIdCEIdWarehouse(int purchaseOrderVersionId, int plantVersionId){
		return (Double)costsByPOWarehouse.get(purchaseOrderVersionId + "|" + plantVersionId);
	}
	

	
	/**
	 * @return the successful
	 */
	public boolean isSuccessful() {
		return successful;
	}

	/**
	 * @return the errorMsg
	 */
	public String getErrorMsg() {
		return errorMsg;
	}
}
